import { ref, reactive, computed } from 'vue'
import { getGoodsClassify, getGoodsClassifyTwo } from '@/common/request/api'
import type { ClassifyItem } from '@/common/request/api'
import {
	useUserLocation,
	useCurrentInstance
} from '.'
import { useI18n } from "vue-i18n";
export const useNearby = (cb ?: Function) => {
	const ctx = useCurrentInstance()
	const { t } = useI18n()
	const {
		getLocation } = useUserLocation()
	

	const showObject = reactive({
		allShow: false,
		sortShow: false,
		filtrateShow: false,
	})
	type FilterItem = {
		id : number
		name : string
		isCheck : boolean
		ifShow : boolean
		type : string
	}
	const filterList = ref<FilterItem[]>([
		{
			id: 1,
			name: t('common.全部'),
			isCheck: false,
			ifShow: false,
			type: 'allShow',
		},
		{
			id: 2,
			name: t('nearby.智能排序'),
			isCheck: false,
			ifShow: false,
			type: 'sortShow',
		},
		{
			id: 3,
			name: t('nearby.筛选'),
			isCheck: false,
			ifShow: false,
			type: 'filtrateShow',
		}
	])

	const oneClassifyList = ref<ClassifyItem[]>([])
	const twoClassifyList = ref<ClassifyItem[]>([])
	const allClassifyList = computed(() => {
		return [
			{
				icon: ctx.$staticPath('nearby-all.png'),
				id: -1,
				name: t('common.全部'),
			},
			...oneClassifyList.value
		]
	})
	const categoryOne = ref(-1)

	const handleChangeCategory = (val : number) => {
		if (val !== -1) {
			getGoodsClassifyTwo(val).then(res => {
				twoClassifyList.value = res.data
			})
		}
	}
	let reset : Function
	const setResetFn = (fn : Function) => {
		reset = fn
	}


	const selectCategoryIndex = ref(0)
	const currentCategory = computed(() => {
		return allClassifyList.value[selectCategoryIndex.value]
	})

	const handleSelectCategory = (item : ClassifyItem, index : number) => {
		selectCategoryIndex.value = index
		filterList.value[0].name = item.name
		reset?.()
	}




	const hanldeChangeTab = (item : FilterItem) => {
		showObject.allShow = item.type === 'allShow'
		showObject.sortShow = item.type === 'sortShow'
		showObject.filtrateShow = item.type === 'filtrateShow'
	}

	const sortObj = ref<{
		orderByType ?: number
		orderByCollation ?: number
	}>({})
	const handleChangeSort = (item : { id : number, name : string }) => {
		// orderByType	4	否	string	排序类型 1综合 2销量 3评分 4距离 5价格
		// orderByCollation	1	否	string	排序规则 1升序 2降序
		filterList.value[1].name = item.name
		switch (item.id) {
			case 1:
				sortObj.value = {
					orderByType: 1
				}
				break
			case 2:
				sortObj.value = {
					orderByType: 4,
					orderByCollation: 1
				}
				break
			case 3:
				sortObj.value = {
					orderByType: 3,
					orderByCollation: 2
				}
				break
			case 4:
				sortObj.value = {
					orderByType: 2,
					orderByCollation: 2
				}
				break
			case 5:
				sortObj.value = {
					orderByType: 5,
					orderByCollation: 1
				}
				break
			case 6:
				sortObj.value = {
					orderByType: 5,
					orderByCollation: 2
				}
				break
		}
		reset?.()
	}
	const filterObj = ref<{
		beginTime ?: string
		endingTime ?: string
	}>({

	})
	const handleChangeFilter = (time : string[]) => {
		const t1 = time[0].split(":")
		const t2 = time[1].split(":")
		filterObj.value = {
			beginTime: t1[0],
			endingTime: t2[0]
		}
		reset?.()
	}

	uni.showLoading()
	getGoodsClassify({
		type: 2
	}).then(res => {
		oneClassifyList.value = res.data
		cb?.()
	}).finally(() => {
		uni.hideLoading()
	})

	return {
		getLocation,
		showObject,
		filterList,
		allClassifyList,
		oneClassifyList,
		twoClassifyList,
		currentCategory,
		handleSelectCategory,
		hanldeChangeTab,
		sortObj,
		handleChangeSort,
		filterObj,
		handleChangeFilter,
		categoryOne,
		handleChangeCategory,
		setResetFn
	}
}